package com.whcoding.rabbitmq.controller;

import com.alibaba.fastjson.JSONObject;
import com.whcoding.rabbitmq.constants.RabbitConsts;
import com.whcoding.rabbitmq.example.common.GZipUtils;
import com.whcoding.rabbitmq.message.MessageStruct;
import com.whcoding.rabbitmq.pojo.OrmDeptVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @program: spring-boot-learning
 * @description:
 * @author: SnailThink
 * @create: 2021-10-27 16:13
 **/
@RestController
public class SendMessageController {

	private static final Logger logger = LoggerFactory.getLogger(SendMessageController.class);

	//使用RabbitTemplate,这提供了接收/发送等等方法
	@Autowired
	RabbitTemplate rabbitTemplate;

	/**
	 * 测试 直接模式发送
	 * direc 直连式不需要 routingKey
	 *
	 * @return
	 */
	@GetMapping("/sendDirectMessage")
	public String sendDirectMessage() {
		rabbitTemplate.convertAndSend(RabbitConsts.DIRECT_MODE_QUEUE_ONE, new MessageStruct("direct message"));
		return "ok";
	}

	/**
	 * 测试分列模式发送
	 *
	 * @return
	 */
	@GetMapping("/sendDirectMessage2")
	public void sendDirectMessage2() {
		String messageId = String.valueOf(UUID.randomUUID());
		String messageData = "test message, hello!";
		String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		Map<String, Object> map = new HashMap<>(16);
		map.put("messageId", messageId);
		map.put("messageData", messageData);
		map.put("createTime", createTime);
		//将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
		rabbitTemplate.convertAndSend(RabbitConsts.FANOUT_MODE_QUEUE, "", map);
	}


	/**
	 * 测试主题模式发送1
	 *
	 * @return
	 */
	@GetMapping("/sendDirectMessage3")
	public void sendDirectMessage3() {
		rabbitTemplate.convertAndSend(RabbitConsts.TOPIC_MODE_QUEUE, "queue.aaa.bbb", new MessageStruct("topic message"));
	}


	/**
	 * 测试主题模式发送2
	 *
	 * @return
	 */
	@GetMapping("/sendDirectMessage4")
	public void sendDirectMessage4() {
		rabbitTemplate.convertAndSend(RabbitConsts.TOPIC_MODE_QUEUE, "ccc.queue", new MessageStruct("topic message"));
	}


	/**
	 * 测试主题模式发送3
	 *
	 * @return
	 */
	@GetMapping("/sendDirectMessage5")
	public void sendDirectMessage5() {
		OrmDeptVO ormDeptVO=new OrmDeptVO();
		ormDeptVO.setDeptId(100);
		ormDeptVO.setDeptName("信息部");
		ormDeptVO.setDeptNo("X1001");
		String jsonStr=JSONObject.toJSONString(ormDeptVO);
		System.out.println(jsonStr);
		MessageStruct messageStruct=new MessageStruct(jsonStr);
//		rabbitTemplate.convertAndSend("order.exchange.centre", "order.send.routingkey",messageStruct);
		String zStr=GZipUtils.gzip(jsonStr);

		byte[] bytes=jsonStr.getBytes(StandardCharsets.UTF_8);
		rabbitTemplate.convertAndSend("order.exchange.centre", "order.sign.routingkey",bytes);
	}



	/**
	 * 发送消息 包含 routingKey 以及exchangeName
	 *
	 * @param exchangeName
	 * @param routingKey
	 * @param message
	 */
	public void sendMessage(String exchangeName, String routingKey, String message) {

		Message messageBean = MessageBuilder.withBody(message.getBytes())
				.setHeader("routingKey", routingKey)
				.build();
		//推送mq
		rabbitTemplate.convertAndSend(exchangeName, routingKey, messageBean);
		logger.info("推送mq消息routingKey:{}，内容：{}", routingKey, message);
	}
}
